//
//  SGJTaskController.m
//  Sewage
//
//  Created by 宗庆猛 on 2017/5/26.
//  Copyright © 2017年 宗庆猛. All rights reserved.
//

#import "SGJTaskController.h"
#import "SGJNetworkTools.h"

#import <YYModel.h>
#import "SGJGrabOrderModel.h"
#import "SGJTaskCell.h"

#import "SGJTaskExamineDetailViewController.h"

@interface SGJTaskController ()<SGJTaskCellDelegate,UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) NSArray *currentTaskList;

@property (strong,nonatomic) UITableView *tableView;

@end

@implementation SGJTaskController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"任务";
    
    [self loadData];
    
    [self setupTableView];
}

-(void)setupTableView{
    
    CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120);
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = CGRectMake(0, 0, frame.size.width, [UIScreen mainScreen].bounds.size.height);
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
//    SGJTaskDetailHeader *header = [[NSBundle mainBundle] loadNibNamed:@"SGJTaskDetailHeader" owner:nil options:nil].lastObject;
//    header.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 145);
//    tableView.tableHeaderView = header;
    tableView.tableFooterView = [[UIView alloc] init];
    //[tableView registerClass:[SGJGrabOrderCell class] forCellReuseIdentifier:@"grabOrder"];
    [tableView registerNib:[UINib nibWithNibName:@"SGJTaskCell" bundle:nil] forCellReuseIdentifier:@"SGJTaskCell"];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.currentTaskList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SGJTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SGJTaskCell" forIndexPath:indexPath];
    
    cell.grabOrderModel = self.currentTaskList[indexPath.row];
    
    //cell.poiModel = self.poiRecordsList[indexPath.row];
    //cell.grabOrderModel = self.grabOrderList[indexPath.row];
    // Configure the cell...
    
     cell.delegate = self;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 190;
}

#pragma mark - SGJTaskCellDelegate
-(void)quitTaskWithTaskCell:(SGJTaskCell *)taskCell{
    NSLog(@"放弃任务%@",taskCell);
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"userId"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    params[@"taskId"] = taskCell.grabOrderModel.id;
    [[SGJNetworkTools sharedNetworkTool] quitTaskWithUrlString:kQuitTaskUrl parameters:params method:@"POST" callBack:^(id responseObject) {
        NSLog(@"放弃任务%@",responseObject);
    }];
}

-(void)startTaskWithTaskCell:(SGJTaskCell *)taskCell{
    NSLog(@"开始巡检%@",taskCell);//应该是没有接口，直接跳转
    SGJTaskExamineDetailViewController *examineVc = [[SGJTaskExamineDetailViewController alloc] init];
    examineVc.grabOrderModel = taskCell.grabOrderModel;
    [self.navigationController pushViewController:examineVc animated:YES];
    
}

-(void)loadData{
    //根据条件查询用户任务集合	/api/v1/loadUserTaskList	{userId:1111,taskId:2222}
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"userId"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    params[@"taskId"] = @"";//首次不传，上拉加载更多的时候， 才需要传这个参数
    [[SGJNetworkTools sharedNetworkTool] loadCurrentTaskWithUrlString:kTaskListByUserIdUrl parameters:params method:@"POST" callBack:^(id responseObject) {
        NSLog(@"当前任务list%@",responseObject);
        
        self.currentTaskList = [NSArray yy_modelArrayWithClass:[SGJGrabOrderModel class] json:responseObject[@"retData"]];
        
        [self.tableView reloadData];
        
    }];
}

@end
